package zpx.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.compass.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zpx.dao.MaterialDao;
import zpx.iservice.IComAttachmentService;
import zpx.iservice.IMaterialService;
import zpx.model.Material;
import zpx.model.User;

@Service("materialService")
public class MaterialService implements IMaterialService{


 private  MaterialDao materialDao;
 
 public MaterialDao getMaterialDao() {
     return materialDao;
 }

 @Autowired
 public void setMaterialDao(MaterialDao materialDao) {
     this.materialDao = materialDao;
 }
 
 @Autowired
 private IComAttachmentService comAttachmentService;
    
    /**
     * 获得物料数量
     */
 	@Override
     public int getMaterialCount(Map<String, String> searchMap) {
     return materialDao.getMaterialCount(searchMap);
    }

    /**
     * 获得物料列表
     * @param searchMap
     * @return
     */
    @Override
    public List<Material> getMaterialsList(Map<String, String> searchMap) {
     return materialDao.getMaterialsList(searchMap);
    }
    
    /**
     * 根据主键获得物料对象
     * 
     * @param id 物料对象主键
     * @return 单个物料对象
     */
    @Override
    public Material getMaterial(String id) {
     
     return materialDao.getMaterial(id);
    }
    
    /**
     * 保存更新物料对象
     * 
     * @param material 物料对象
     * @return 物料对象
     */
    @Override
    public Material saveOrUpdateMaterial(Material material) {
     
     return materialDao.saveOrUpdateMaterial(material);
    }
    
    /**
     * 保存物料信息
     */
	@Override
	public String saveOrUpdate(Map<String, Object> map) {
		Material material=(Material) map.get("material");
		User loginUser=(User) map.get("loginUser");
		
		//修改
		if(StringUtils.hasText(material.getId())){
			material.setUpdateDate(new Date());
			material.setUpdatePerson(loginUser.getId());
		}else{
			//添加
			material.setCreateDate(new Date());
			material.setCreatePerson(loginUser.getId());
		}
		materialDao.saveOrUpdateMaterial(material);	
		String fileNames=(String) map.get("fileNames");
		String filePaths=(String) map.get("filePaths");
		String delComAttIds=(String) map.get("delComAttIds");
		
		// 保存附件
        if (fileNames != null && !"".equals(fileNames.trim()) && filePaths != null
                && !"".equals(filePaths.trim())) {
        	comAttachmentService.saveComAttachment(fileNames, filePaths,
        			material.getId(),loginUser);
        }
        // 删除附件
        if (delComAttIds != null && !"".equals(delComAttIds.trim())) {
        	comAttachmentService.deleteComAttachments(delComAttIds,loginUser);
        }
		
		return null;
	}
    
    /**
     * 删除物料
     * 
     * @param id 物料主键
     * @return 
     */
    @Override
    public void deleteMaterial(String id) {
     materialDao.deleteMaterial(id);
    }

   
    
}
